package com.qqtech.qquan.mall.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.mall.enums.MallConst;
import com.qqtech.qquan.mall.enums.MallSupplierStatusDictEnum;
import com.qqtech.qquan.mall.model.MallSupplier;
import com.qqtech.qquan.mall.model.MallSupplierQuery;
import com.qqtech.qquan.mall.model.MallSupplierVo;
import com.qqtech.qquan.mall.service.MallSupplierService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/mall/supplier")
public class MallSupplierController extends BaseWebController<MallSupplier, MallSupplierQuery> {
	@Resource
	private MallSupplierService mallSupplierService;
    
	@Override
	protected BaseService<MallSupplier> getBaseService() {
		return mallSupplierService;
	}
	
	/**
	 * 获取供应商列表,(封装，每行显示两个)
	 * 
	 * @param mallSupplierQuery
	 * @return
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public Result list(@RequestBody MallSupplierQuery mallSupplierQuery) {
		Result result = new Result();
		Pageable pageKit = new PageKit(new PageOrder(Direction.ASC, "oidx"));
		mallSupplierQuery.setStatus(MallSupplierStatusDictEnum.NORMAL.getCode());
		Page<MallSupplierVo> page = mallSupplierService.queryPageList(mallSupplierQuery, pageKit);
		List<MallSupplierVo> list = page.getContent();
		if(list != null && list.size() > 0){
			LinkedHashMap<String, List<MallSupplierVo>> supplierMap =  new LinkedHashMap<String, List<MallSupplierVo>>();
			int startIndex = 0;
			if(mallSupplierQuery.getPage() != null && mallSupplierQuery.getPage() > 1){
				startIndex = (mallSupplierQuery.getPage()-1) * 10;
			}
			for (int i = 0 ; i < list.size(); i++) {
				MallSupplierVo mallSupplierVo = new MallSupplierVo();
				mallSupplierVo = list.get(i);
				//封装
				int key = startIndex/2 + 1;
				String keyStr = key+"";
				if(supplierMap.containsKey(keyStr)){
					List<MallSupplierVo> productList = supplierMap.get(keyStr);
					productList.add(mallSupplierVo);
				}else{
					List<MallSupplierVo> newproductList = new ArrayList<MallSupplierVo>();
					newproductList.add(mallSupplierVo);
					supplierMap.put(keyStr, newproductList);
				}
				startIndex++;
			}
			result.put("supplierMap", supplierMap);
			result.put("totalPages", page.getTotalPages());
			result.put("contentSize", page.getContent().size());
		}
		
		//result.put("page", page);
		return result;
	}
	
	/**
	 * 详情
	 * 
	 * @param mallSupplierQuery
	 * @return
	 */
	@RequestMapping(value = "/detail")
	@ResponseBody
	public Result detail(@RequestBody MallSupplierQuery mallSupplierQuery) {
		Result result = new Result();
		Integer supplierId = mallSupplierQuery.getId();
		if (supplierId == null || supplierId == 0) {
			result.addErr("供应商id为空！");
			return result;
		}
		MallSupplierVo mallSupplier = mallSupplierService.getById(supplierId);
		if(mallSupplier != null && StringUtil.isNotBlank(mallSupplier.getResumeImg())){
			mallSupplier.setImgString(mallSupplier.getResumeImg(), MallConst.IMG_RESUME);
		}
		result.put("mallSupplier", mallSupplier);
		return result;
	}
	
	/**
	 * banner列表
	 * 
	 * @param mallSupplierQuery
	 * @return
	 */
	@RequestMapping(value = "/showList")
	@ResponseBody
	public Result showList(@RequestBody MallSupplierQuery mallSupplierQuery) {
		Result result = new Result();
		//banner
		Pageable pageKit = new PageKit(1, new PageOrder(Direction.ASC, "id"));
		mallSupplierQuery.setStatus(MallSupplierStatusDictEnum.NORMAL.getCode());
		mallSupplierQuery.setIsShow(YesNoEnum.YES.getCode());
		List<MallSupplierVo> showList = mallSupplierService.queryList(mallSupplierQuery, pageKit);
		List<Map<String, Object>> bannerList = new ArrayList<Map<String, Object>>();
		if(showList != null && showList.size() > 0){
			for(MallSupplierVo supplier : showList){
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("template", "0");
				map.put("imgs", supplier.getImgHandleDomain() + supplier.getBanner() + MallConst.IMG_SUPPLIER_BANNER);
				map.put("path", "source://view/mall/supplier/main.ui");
				map.put("jsonData", supplier);
				bannerList.add(map);
			}
			result.put("bannerList", bannerList);
		}
		return result;
	}
}
